/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.web;

import com.jeeplus.modules.guide.service.RongYiBanService;
import com.jeeplus.modules.guide.task.RongYiBan;
import com.jeeplus.modules.guide.utils.RongYiBanOkHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
蓉易办对接排队叫号
*/
@RestController
@Api(tags = "蓉易办对接叫号")
@RequestMapping("/rongYiBanTiming")
@RequiredArgsConstructor
public class RongYiBanTiming {
    //蓉易办对接Appkey
    @Value("${AppKey}")
    private String AppKey;
    //蓉易办-排队叫号-区县创建大厅
    @Value("${Hall}")
    private String Hall;
    //蓉易办-排队叫号-区县创建窗口
    @Value("${Window}")
    private String Window;
    //蓉易办-排队叫号-区县创建事项分类
    @Value("${EventClassification}")
    private String EventClassification;
    //蓉易办-排队叫号-区县上传取号数据
    @Value("${GetNumber}")
    private String GetNumber;
    //蓉易办-排队叫号-区县取号办理信息上传
    @Value("${HandleInfoUp}")
    private String HandleInfoUp;
    //高新行政区划代码
    private String AreaCode="510109";

    private final RongYiBanService rongYiBanService;
    private final RongYiBan rongYiBan;
	private static final Logger log = LoggerFactory.getLogger(RongYiBanTiming.class);


	/**
	 * 蓉易办-排队叫号-区县创建大厅
	 */
	@GetMapping(value = "establishSala")
    public void establishSala(){
		log.error("蓉易办-排队叫号-区县创建大厅");
//        List<Map<String, String>> list = rongYiBanService.establishSala();
//        for (int i = 0; i < list.size(); i++) {
//            List<Map<String,String>> mapList=new ArrayList<>();
//            mapList.add(list.get(i));
//            JSONObject params = new JSONObject();
//            params.put("halllist",mapList);
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("areacode",AreaCode);
//            jsonObject.put("params",params);
//            System.out.println(jsonObject);
//            String data = RongYiBanOkHttpUtil.getData(AppKey, Hall, jsonObject.toString());
        String data = RongYiBanOkHttpUtil.getData(AppKey, Hall, "{\"areacode\":\"510109\",\"params\":{\"halllist\":[{\"hallname\":\"4F-B区公安分局\",\"hallguid\":\"5101090000008\"}]}}");
//            log.error(data);
//        }

    }

    /**
     * 蓉易办-排队叫号-区县创建窗口
     */
    @GetMapping(value = "establishWindow")
    @ApiOperation(value ="蓉易办-排队叫号-区县创建窗口")
    public void establishWindow(){
        log.error("蓉易办-排队叫号-区县创建窗口");
        List<Map<String, String>> salaList = rongYiBanService.establishSala();
        for (int i = 0; i < salaList.size(); i++) {
            List<Map<String, String>> windowList = rongYiBanService.establishWindow(salaList.get(i).get("hallguid"));
            JSONObject params = new JSONObject();
            params.put("windowlist",windowList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("areacode",AreaCode);
            jsonObject.put("params",params);
            String data =RongYiBanOkHttpUtil.getData(AppKey, Window, jsonObject.toString());
            log.error(data);
        }

    }

    /**
     * 蓉易办-排队叫号-区县创建事项分类
     */
    @GetMapping(value = "establishSerialInfo")
    @ApiOperation(value ="蓉易办-排队叫号-区县创建事项分类(业务)")
    public void establishSerialInfo(){
        log.error("蓉易办-排队叫号-区县创建事项分类(业务)");
        List<Map<String, String>> salaList = rongYiBanService.establishSala();
        for (int i = 0; i < salaList.size(); i++) {
            List<Map<String,String>> serialList=new ArrayList<>();
            List<Map<String, String>> list = rongYiBanService.getWindowSerial(salaList.get(i).get("hallguid"));
            for (int j = 0; j < list.size(); j++) {
                if(null!=list.get(j).get("serial1") && !"".equals(list.get(j).get("serial1"))){
                    serialList.addAll(splitString(list.get(j).get("serial1"),salaList.get(i).get("hallguid"),list.get(j).get("windowguid")));
                }
                if(null!=list.get(j).get("serial2") && !"".equals(list.get(j).get("serial2"))){
                    serialList.addAll(splitString(list.get(j).get("serial2"),salaList.get(i).get("hallguid"),list.get(j).get("windowguid")));
                }
                if(null!=list.get(j).get("serial3") && !"".equals(list.get(j).get("serial3"))){
                    serialList.addAll(splitString(list.get(j).get("serial3"),salaList.get(i).get("hallguid"),list.get(j).get("windowguid")));
                }
                if(null!=list.get(j).get("serial4") && !"".equals(list.get(j).get("serial4"))){
                    serialList.addAll(splitString(list.get(j).get("serial4"),salaList.get(i).get("hallguid"),list.get(j).get("windowguid")));
                }
                if(null!=list.get(j).get("serial5") && !"".equals(list.get(j).get("serial5"))){
                    serialList.addAll(splitString(list.get(j).get("serial5"),salaList.get(i).get("hallguid"),list.get(j).get("windowguid")));
                }
            }
            for (int k = 0; k < serialList.size(); k++) {
                serialList.get(k).put("tasktypeguid",AreaCode+serialList.get(k).get("tasktypeguid"));
            }
            for (int k = 0; k < serialList.size(); k++) {
                JSONObject params = new JSONObject();
                JSONArray jsonArray=new JSONArray();
                jsonArray.add(serialList.get(k));
                params.put("tasktypelist",jsonArray);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("areacode",AreaCode);
                jsonObject.put("params",params);
                log.error("蓉易办-排队叫号-区县创建事项分类(业务):"+jsonObject.toString());
                String data =RongYiBanOkHttpUtil.getData(AppKey, EventClassification, jsonObject.toString());
                log.error("蓉易办-排队叫号-区县创建事项分类(业务)接口返回:"+data);
            }

//            String data =RongYiBanOkHttpUtil.getData(AppKey, EventClassification, jsonObject.toString());
//            log.error(data);

        }
    }
    /**
     * 蓉易办-排队叫号-区县取号办理信息上传
     */
    @GetMapping(value = "estabgetIsCallQH")
    @ApiOperation(value ="蓉易办-排队叫号-总")
    public void estabgetIsCallQH(){
        rongYiBan.RongYiBanQH();


    }
    /**
     * 蓉易办-排队叫号-区县取号办理信息上传
     */
    @GetMapping(value = "estabgetIsCallJH")
    @ApiOperation(value ="蓉易办-排队叫号-总")
    public void estabgetIsCallJH(){

        rongYiBan.RongYiBanJH();


    }
    /**
     * 蓉易办-排队叫号-区县取号办理信息上传
     */
    @GetMapping(value = "estabgetIsCallBJ")
    @ApiOperation(value ="蓉易办-排队叫号-总")
    public void estabgetIsCallBJ(){

        rongYiBan.RongYiBanBJ();

    }



    /**
     * 业务拆分
     * @param serial 业务编号
     * @param hallguid 区域编号
     * @param windowguid 窗口ID
     * @return
     */
    public List<Map<String, String>> splitString(String serial ,String hallguid,String windowguid) {
        List<Map<String, String>> list =new ArrayList<>();
        String[] serial1s = serial.split("-");
        for (int k = 0; k < serial1s.length; k++) {
            Map<String, String> map = new HashMap<>();
            map.put("areacode", hallguid);
            map.put("serial", serial1s[k]);
            map.put("windowguid", windowguid);
             list .addAll( rongYiBanService.establishSerialInfo(map));
        }
        return list;
    }

}
